package com.atguigu.gulimall.order.service.impl;

import com.atguigu.gulimall.order.entity.OrderReturnReasonEntity;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.order.dao.OrderItemDao;
import com.atguigu.gulimall.order.entity.OrderItemEntity;
import com.atguigu.gulimall.order.service.OrderItemService;

@Slf4j
@Service("orderItemService")
public class OrderItemServiceImpl extends ServiceImpl<OrderItemDao, OrderItemEntity> implements OrderItemService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<OrderItemEntity> page = this.page(
                new Query<OrderItemEntity>().getPage(params),
                new QueryWrapper<OrderItemEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 三种接收消息的方式message,OrderReturnReasonEntity,Channel
     * 改为手动确认状态，即使服务宕机，消息也不会丢失，acknowledge-mode=manual
     *
     * @param message
     * @param orderReturnReasonEntity
     * @param channel
     */
    @RabbitListener(queues = {"helloQueue"})
    public void receiveMessage(Message message, OrderReturnReasonEntity orderReturnReasonEntity, Channel channel) throws IOException {
        byte[] body = message.getBody();
        log.info("接收到的消息内容为：{}" + body + orderReturnReasonEntity);
        long deliveryTag = message.getMessageProperties().getDeliveryTag();//channel内自增
        if (deliveryTag % 2 == 1) {
            channel.basicAck(deliveryTag, true);  //false非批量，一个一个签收确认
            System.out.println("确认签收了deliveryTag..." + deliveryTag);
        } else {
            channel.basicNack(deliveryTag, false, true);
            System.out.println("没有签收..." + deliveryTag);
        }

    }

}